﻿using System;
using ParserGenerator.GrammarConstruction;

namespace ParserGenerator.ParserConstruction
{
    [Serializable]
    public sealed class ParserAutomatonReduction : ParserAutomatonContribution
    {
        public GrammarProduction Production { get; }
        public ParserAutomatonItem Item => Production.ReduceItem;

        public ParserAutomatonReductionLookaheadSet LookaheadSet
        {
            get
            {
                if (State.IsConsistent)
                    return null;
                var lookaheadSets = State.Table.ReductionLookaheadSets[State];
                var lookaheadSet = lookaheadSets[Production];
                return lookaheadSet;
            }
        }

        internal ParserAutomatonReduction(ParserAutomatonState state, GrammarProduction production)
            : base(state)
        {
            if (production == null)
                throw new ArgumentNullException(nameof(production));
            Production = production;
        }

        public sealed override string ToString()
        {
            if (LookaheadSet == null)
                return Production + " .";
            else
                return Production + " . " + LookaheadSet;
        }
    }
}
